package com.lwb.ztx.news.widget;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.lwb.ztx.R;
import com.lwb.ztx.bean.NewsBean;
import com.lwb.ztx.news.widget.content.NewsContent;
import com.lwb.ztx.utils.ImageLoaderUtils;
import com.lwb.ztx.utils.LogUtils;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * TODO: Replace the implementation with code for your data type.
 */
public class NewsAdapter extends RecyclerView.Adapter{

    private static final int TYPE_NORMAL = 1;
    private static final int TYPE_3_IMAGE = 2;
    private static final int TYPE_SHORT_NEWS = 3;

    private NewsContent content;
    private Context context;
    private OnItemClickListener listener;

    public void setListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    public NewsAdapter(Context context, NewsContent content) {
        this.content = content;
        this.context = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(viewType == TYPE_NORMAL) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.news_item_normal, parent, false);
            return new NormalViewHolder(view);
        } else if(viewType == TYPE_3_IMAGE) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.news_item_3_image, parent, false);
            return new ThreeImageViewHolder(view);
        } else if(viewType == TYPE_SHORT_NEWS) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.news_item_short_news, parent, false);
            return new ShortNewsViewHolder(view);
        }

        else {
            View view = new View(context);
            return new DefalutViewHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        if(holder instanceof NormalViewHolder) {
            NormalViewHolder normalViewHolder = (NormalViewHolder)holder;
            NewsBean bean = content.getIndex(position);
            if(bean.getThumbnail().equals("")) {
                normalViewHolder.mNewsImage.setVisibility(View.GONE);
            } else {
                normalViewHolder.mNewsImage.setVisibility(View.VISIBLE);
                ImageLoaderUtils.display(context, normalViewHolder.mNewsImage, bean
                        .getThumbnail());
            }
            normalViewHolder.mNewsTitle.setText(bean.getTitle());
            normalViewHolder.mNewsSource.setText(bean.getUpdateTime());
            normalViewHolder.mNewsComments.setText(bean.getCommentsAll() + "条评论");
            normalViewHolder.content = bean;
            if(bean.getType().equals("topic2")) {
                normalViewHolder.mNewsTopic.setVisibility(View.VISIBLE);
            } else {
                normalViewHolder.mNewsTopic.setVisibility(View.GONE);
            }
        } else if(holder instanceof ThreeImageViewHolder) {
            ThreeImageViewHolder threeImageViewHolder = (ThreeImageViewHolder) holder;
            NewsBean bean = content.getIndex(position);
            LogUtils.d("Test:" + (null == threeImageViewHolder.mNewsTitle));
            threeImageViewHolder.mNewsTitle.setText(bean.getTitle());
            threeImageViewHolder.mNewsSource.setText(bean.getUpdateTime());
            threeImageViewHolder.mNewsComments.setText(bean.getCommentsAll() + "条评论");
            threeImageViewHolder.content = bean;
            if (bean.getImages() != null)
            for(int i=0;i<bean.getImages().size();i++) {
                if(i >= 3) {
                    continue;
                }
                ImageLoaderUtils.display(context, threeImageViewHolder.imageViews[i], bean.getImages().get(i));
            }
        } else if(holder instanceof ShortNewsViewHolder) {
            NewsBean bean = content.getIndex(position);
            ShortNewsViewHolder shortNewsViewHolder = (ShortNewsViewHolder) holder;
            ImageLoaderUtils.display(context, shortNewsViewHolder.shortImage, bean.getThumbnail());
            shortNewsViewHolder.content = bean;
        }
    }

    @Override
    public int getItemCount() {
        return content.getSize();
    }

    @Override
    public int getItemViewType(int position) {
        if(content.getIndex(position).getType().equals("slide")) {
            return TYPE_3_IMAGE;
        } else if(content.getIndex(position).getType().equals("shortNews")) {
            return TYPE_SHORT_NEWS;
        }
        else {
            return TYPE_NORMAL;
        }
    }

    public class ShortNewsViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        public ImageView shortImage;
        public NewsBean content;
        public ShortNewsViewHolder(View itemView) {
            super(itemView);
            itemView.setOnClickListener(this);
            shortImage = (ImageView) itemView.findViewById(R.id.news_item_short_news);
        }

        @Override
        public void onClick(View v) {
            if(null != listener) {
                listener.onItemClick(content);
            }
        }
    }

    public class ThreeImageViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        public ImageView mNewsImage1;
        public ImageView mNewsImage2;
        public ImageView mNewsImage3;
        public TextView mNewsTitle;
        public TextView mNewsSource;
        public TextView mNewsComments;
        public ImageView imageViews[];
        public NewsBean content;
        public ThreeImageViewHolder(View itemView) {
            super(itemView);
            itemView.setOnClickListener(this);
            mNewsImage1 = (ImageView) itemView.findViewById(R.id.news_item_3_image_1);
            mNewsImage2 = (ImageView) itemView.findViewById(R.id.news_item_3_image_2);
            mNewsImage3 = (ImageView) itemView.findViewById(R.id.news_item_3_image_3);
            mNewsTitle = (TextView) itemView.findViewById(R.id.news_item_3_image_title);
            mNewsSource = (TextView) itemView.findViewById(R.id.news_item_3_image_time);
            mNewsComments = (TextView) itemView.findViewById(R.id.news_item_3_image_comments);
            imageViews = new ImageView[]{mNewsImage1, mNewsImage2, mNewsImage3};
        }
        @Override
        public void onClick(View v) {
            if(null != listener) {
                listener.onItemClick(content);
            }
        }
    }

    public class NormalViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        public View mView;
        @InjectView(R.id.news_item_image)
        public ImageView mNewsImage;
        @InjectView(R.id.news_item_title)
        public TextView mNewsTitle;
        @InjectView(R.id.news_item_source)
        public TextView mNewsSource;
        @InjectView(R.id.news_item_comments)
        public TextView mNewsComments;
        @InjectView(R.id.news_item_topic)
        public ImageView mNewsTopic;

        public NewsBean content;

        public NormalViewHolder(View view) {
            super(view);
            mView = view;
            mView.setOnClickListener(this);
            ButterKnife.inject(this, view);
        }

        @Override
        public void onClick(View v) {
//            Toast.makeText(context,"url " + url, Toast.LENGTH_LONG).show();
            if(null != listener) {
                listener.onItemClick(content);
            }
        }
    }

    class  DefalutViewHolder extends RecyclerView.ViewHolder{

        public DefalutViewHolder(View itemView) {
            super(itemView);
        }
    }

    public interface OnItemClickListener {
        void onItemClick(NewsBean bean);
    }

}
